package me.zhengjie.pm.projectissue.service.impl;

import me.zhengjie.pm.projectissue.domain.ProjectIssue;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.pm.projectissue.repository.ProjectIssueRepository;
import me.zhengjie.pm.projectissue.service.ProjectIssueService;
import me.zhengjie.pm.projectissue.service.dto.ProjectIssueDto;
import me.zhengjie.pm.projectissue.service.dto.ProjectIssueQueryCriteria;
import me.zhengjie.pm.projectissue.service.mapper.ProjectIssueMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author yuxj
* @date 2020-05-02
*/
@Service
//@CacheConfig(cacheNames = "projectIssue")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectIssueServiceImpl implements ProjectIssueService {

    private final ProjectIssueRepository projectIssueRepository;

    private final ProjectIssueMapper projectIssueMapper;

    public ProjectIssueServiceImpl(ProjectIssueRepository projectIssueRepository, ProjectIssueMapper projectIssueMapper) {
        this.projectIssueRepository = projectIssueRepository;
        this.projectIssueMapper = projectIssueMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ProjectIssueQueryCriteria criteria, Pageable pageable){
        Page<ProjectIssue> page = projectIssueRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(projectIssueMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ProjectIssueDto> queryAll(ProjectIssueQueryCriteria criteria){
        return projectIssueMapper.toDto(projectIssueRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ProjectIssueDto findById(Integer id) {
        ProjectIssue projectIssue = projectIssueRepository.findById(id).orElseGet(ProjectIssue::new);
        ValidationUtil.isNull(projectIssue.getId(),"ProjectIssue","id",id);
        return projectIssueMapper.toDto(projectIssue);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ProjectIssueDto create(ProjectIssue resources) {
        return projectIssueMapper.toDto(projectIssueRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectIssue resources) {
        ProjectIssue projectIssue = projectIssueRepository.findById(resources.getId()).orElseGet(ProjectIssue::new);
        ValidationUtil.isNull( projectIssue.getId(),"ProjectIssue","id",resources.getId());
        projectIssue.copy(resources);
        projectIssueRepository.save(projectIssue);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            projectIssueRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProjectIssueDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectIssueDto projectIssue : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("问题发生舰船", projectIssue.getShip());
            map.put("问题舰艇型号", projectIssue.getShipType());
            map.put("问题发生项目", projectIssue.getProjectId());
            map.put("问题发生阶段", projectIssue.getProjectStage());
            map.put("问题软件标识", projectIssue.getIssueSoftId());
            map.put("问题软件版本号", projectIssue.getIssueSoftVersion());
            map.put("问题名称", projectIssue.getIssueName());
            map.put("问题描述", projectIssue.getIssueDesc());
            map.put("问题意见答复", projectIssue.getIssueReply());
            map.put("问题种类", projectIssue.getIssueType());
            map.put("严重程度", projectIssue.getIssueSeverity());
            map.put("问题优先级", projectIssue.getPriorityLevel());
            map.put("通报时间", projectIssue.getAvisoTime());
            map.put("通报机关", projectIssue.getAvisoName());
            map.put("问题平台", projectIssue.getIssuePlat());
            map.put("质量问题来源", projectIssue.getIssueSourceType());
            map.put("问题分析", projectIssue.getAnalyse());
            map.put("问题性质", projectIssue.getIssueQuality());
            map.put("问题发现人", projectIssue.getIssueFinderId());
            map.put("问题发现时间", projectIssue.getIssueDiscoveryTime());
            map.put("计划完成时间", projectIssue.getPlanFinishTime());
            map.put("实际解决时间", projectIssue.getIssueFinishTime());
            map.put("处理完成状态", projectIssue.getIssueFinishState());
            map.put("问题所属功能域", projectIssue.getIssueFunctionArea());
            map.put("完成情况说明", projectIssue.getIssueFinishRemark());
            map.put("项目组提出问题时间", projectIssue.getProjectTeamRaiseQuestionsTime());
            map.put("项目组要求完成时间", projectIssue.getProjectTeamRequireFinishTime());
            map.put("项目组确定需求时间", projectIssue.getProjectTeamConfirmDemandTime());
            map.put("需求是否明确", projectIssue.getIsDemandClear());
            map.put("项目组问题修改建议", projectIssue.getProjectTeamSuggest());
            map.put("功能域子模块名称", projectIssue.getModuleName());
            map.put("域负责人", projectIssue.getDomainHeadPerson());
            map.put("问题确认情况", projectIssue.getIssueComfirmType());
            map.put("反馈意见", projectIssue.getFeedback());
            map.put("问题分析及解决措施", projectIssue.getAnalyseSolutions());
            map.put("问题修改情况", projectIssue.getModification());
            map.put("是否发布", projectIssue.getIsRelease());
            map.put("集成情况", projectIssue.getIntegration());
            map.put("验证情况", projectIssue.getVerification());
            map.put("集成验证情况", projectIssue.getIntegrationVerification());
            map.put("测试方", projectIssue.getTestTeam());
            map.put("测试人员", projectIssue.getTesters());
            map.put("测试时间", projectIssue.getTestTime());
            map.put("测试项标志", projectIssue.getTestMark());
            map.put("被测试问题类别", projectIssue.getTestType());
            map.put("测试情况说明", projectIssue.getTestDescribe());
            map.put("问题归零完成状态", projectIssue.getZeroState());
            map.put("质量问题通报时间", projectIssue.getZeroTime());
            map.put("是否质量处问题库问题", projectIssue.getInQualityLibrary());
            map.put("处理后软件版本", projectIssue.getAfterEdition());
            map.put("问题所属变更申请单号", projectIssue.getChangeNum());
            map.put("问题所入库申请单号", projectIssue.getInStorageNum());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}